package com.ruoyi.loan.scheduled;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.loan.domain.CustomerAllocation;
import com.ruoyi.loan.domain.CustomerInfo;
import com.ruoyi.loan.service.ICustomerInfoService;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class CustomerBirthdaySchedule {

    @Autowired
    private ICustomerInfoService customerInfoService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Scheduled(cron = "0 0 1 * * ?")//每天凌晨两点
//    @Scheduled(cron = "*/1 * * * * ?")
    public void CourseWorkDown() {
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        String birthday = sysConfigService.selectConfigByKey("sys.customer.birthday.day.num");
        Calendar birthdayCal = Calendar.getInstance();
        birthdayCal.add(Calendar.DAY_OF_MONTH, StringUtils.isNotBlank(birthday) ? Integer.parseInt(birthday) : 1);
        String date = sdf.format(birthdayCal.getTime());
        QueryWrapper<CustomerInfo> wrapper = Wrappers.query();
        wrapper.or().eq("DATE_FORMAT(birthday,'%m-%d')", date);
        wrapper.or().eq("DATE_FORMAT(spouse_spouse,'%m-%d')", date);
        wrapper.or().eq("DATE_FORMAT(wedding_anniversary,'%m-%d')", date);
        List<CustomerInfo> customerInfoList = this.customerInfoService.list(wrapper);
        if (!CollectionUtils.isEmpty(customerInfoList)) {
            customerInfoList.forEach(t -> {
                t.setFollowRemind(0);
            });
            this.customerInfoService.saveOrUpdateBatch(customerInfoList);
        }
        String repayment = sysConfigService.selectConfigByKey("sys.customer.repayment.day.num");
        Calendar repaymentCal = Calendar.getInstance();
        repaymentCal.add(Calendar.DAY_OF_MONTH, StringUtils.isNotBlank(repayment) ? Integer.parseInt(repayment) : 1);
        int month = repaymentCal.get(Calendar.MONTH) + 1;
        int day = repaymentCal.get(Calendar.DATE);
        List<CustomerInfo> list = this.customerInfoService.list();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(t -> {
                if (ObjectUtil.isNotNull(t.getRepaymentType())) {
                    if (1 == t.getRepaymentType()) {
                        if (ObjectUtil.isNotNull(t.getRepaymentDay()) && day == t.getRepaymentDay()) {
                            t.setFollowRepayment(0);
                        }
                    } else if (2 == t.getRepaymentType()) {
                        if (ObjectUtil.isNotNull(t.getRepaymentDay()) && day == t.getRepaymentDay() && ObjectUtil.isNotNull(t.getRepaymentMonth())) {
                             if (1 == t.getRepaymentMonth() && (1 == month || 4 == month || 7 == month || 10 == month)) {
                                 t.setFollowRepayment(0);
                             } else if (2 == t.getRepaymentMonth() && (2 == month || 5 == month || 8 == month || 11 == month)) {
                                 t.setFollowRepayment(0);
                             } else if (3 == t.getRepaymentMonth() && (3 == month || 6 == month || 9 == month || 12 == month)) {
                                 t.setFollowRepayment(0);
                             }
                        }
                    }
                }
            });
            this.customerInfoService.saveOrUpdateBatch(list);
        }
        System.out.println("定时任务测试");
    }
}
